﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using HTTT.Utilities;
using HTTT.DataAccess;
using HTTT.Bussiness;

namespace HTTT.admin.htdispatch
{
    public partial class EditDispatch : System.Web.UI.Page
    {
        /// <summary>
        ///  View State EditItemID
        /// </summary>
        /// 
        private Guid? EditItemID
        {
            get
            {
                object obj = this.ViewState["_EditItemID"];
                if (obj == null)
                    return null; //default to show ID : 0 
                else
                    return (Guid)obj;
            }
            set
            {
                this.ViewState["_EditItemID"] = value;
            }

        }

        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                LoadData();
                CheckPermission();
            }
        }

        private void LoadData()
        {
            string ID = Request.QueryString["ID"];
            Guid TempID;
            Guid.TryParse(ID, out TempID);
            EditItemID = TempID;
            LoadDispatchDetail();
        }

        private void LoadDispatchDetail()
        {
            HT_DispatchBO BO = new HT_DispatchBO();
            HT_Dispatch current = BO.GetHT_DispatchById((Guid)EditItemID);
            if (current != null)
            {
                txtNo.Text = current.No;
                txtName.Text = current.Name;
                txtSender.Text = current.Sender;
                txtReffer.Text = current.Reffer;
                txtNote.Text = current.Note;
                chkHandingStatus.Checked = (bool)current.Handled;
            }
        }

        private void CheckPermission()
        {
            if (!Page.User.IsInRole(Constants.ROLE_SUPER_ADMINISTRATOR)
                 && !Page.User.IsInRole(Constants.ROLE_ADMINISTRATOR))
            {
                Response.Redirect("/admin/AccessDenied.aspx");
            }
        }

        protected void btnSave_Click(object sender, EventArgs e)
        {
            ReturnValue<bool> Valid = ValidateData();
            if (Valid.RetValue)
            {
                SaveForm();
                //TODO: Show msg
                Response.Redirect("EditDispatch.aspx?id=" + EditItemID);
            }
            else
            {
                ClientScript.RegisterStartupScript(this.GetType(), "Message", Common.FormatAlertMsg(Valid.Msg), true);
            }

        }


        protected void btnSaveClose_Click(object sender, EventArgs e)
        {
            ReturnValue<bool> Valid = ValidateData();
            if (Valid.RetValue)
            {
                bool result = SaveForm();
                if (result)
                {
                    ClientScript.RegisterStartupScript(this.GetType(), "Redirect", Common.FormatMsgThenRedirect("Sửa  xcthành công", "DispatchList.aspx"), true);
                }
                else
                {
                    ClientScript.RegisterStartupScript(this.GetType(), "Redirect", Common.FormatAlertMsg("Có lỗi xảy ra"), true);
                }
            }
            else
            {
                ClientScript.RegisterStartupScript(this.GetType(), "Message", Common.FormatAlertMsg(Valid.Msg), true);
            }

        }
        protected void btnSaveNew_Click(object sender, EventArgs e)
        {
            ReturnValue<bool> Valid = ValidateData();
            if (Valid.RetValue)
            {
                SaveForm();
                EditItemID = null;
                Response.Redirect("AddDispatch.aspx");
            }
            else
            {
                ClientScript.RegisterStartupScript(this.GetType(), "Message", Common.FormatAlertMsg(Valid.Msg), true);
            }

        }
        protected void btnCancel_Click(object sender, EventArgs e)
        {
            Response.Redirect("DispatchList.aspx");
        }

        private ReturnValue<bool> ValidateData()
        {
            if (txtNo.Text.Trim() == string.Empty) return new ReturnValue<bool>(false, "Phải nhập Số công văn yêu cầu");
            if (txtName.Text.Trim() == string.Empty) return new ReturnValue<bool>(false, "Phải nhập tên Công văn yêu cầu");
            if (txtSender.Text.Trim() == string.Empty) return new ReturnValue<bool>(false, "Phải nhập nơi gửi");
            return new ReturnValue<bool>(true, "");
        }


        private bool SaveForm()
        {
            //Collec infomation
            HT_DispatchBO BO = new HT_DispatchBO();
            HT_Dispatch current = BO.GetHT_DispatchById((Guid)EditItemID);
            current.No = txtNo.Text;
            current.Name = txtName.Text;
            current.Sender = txtSender.Text;
            current.Reffer = txtReffer.Text;
            current.Note = txtNote.Text;
            current.Handled = chkHandingStatus.Checked;
            HT_DispatchBO re = new HT_DispatchBO();
            bool result = re.UpdateHT_Dispatch(current);
            return result;
        }
    }
}